<?php

/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */

namespace Xcx\Controller;

use Xcx\Model\QuestionModel;
use Xcx\Model\AccountantModel;
use Xcx\Model\SearchModel;

class IndexController extends XcxBaseController {


    public function banner(){
        $lists = M('banner')->where('is_show = 1 and type = 1')->order('sort_order asc')->field("id,name,path")->select();
        foreach ($lists as &$val){
            $val['path'] = SITE_URL . $val['path'];
        }
        jsonReturn(array('data' => $lists));
    }

    /*
     * 检查有没有新回答
     */
    public function check_response(){
        $bool = getQuestionResponse($this->user_id);
        jsonReturn(array('has_response' => $bool));
    }

    /**
     * 首页问题列表
     */
     public function lists(){
         $questionModel = new QuestionModel();
         $content = I('post.content');
         $where = ' 1=1 ';
         $orderby = '';
         if($content){
             $searchModel = new SearchModel();
             $searchModel->addHistory($content);

             $where .= " and q.title like '%$content%'";
         }

         if($this->user['city']) {
             $city_user = M('users')->where('city = ' . $this->user['city'])->getField('user_id,username');
             $city_user = array_keys($city_user);
             if($city_user) {
                 $orderby = "  (q.user_id in (" . implode(',', $city_user) . ")) desc ";
             }
         }

         if ($orderby){
             $orderby .= ' , q.add_time desc ';
         }else{
             $orderby .= ' q.add_time desc ';
         }

         $page = I('post.page');
         $limit = I('post.limit');

         $count = $questionModel->getList($where,$page,$limit,1,'',true);
         $lists = $questionModel->getList($where,$page,$limit,0,$orderby,true);

         $return['data'] = $lists;
         $return['page'] = $page;
         $return['total'] = $count;
         jsonReturn($return);
     }

    /**
     * 问题详情
     */
    public function detail(){
        $questionModel = new QuestionModel();
        $q_id = I('post.id');
        $find = $questionModel->getQuestionByIdWithAnswer($q_id);
        if($find){
            $accModel = new AccountantModel();
            $hasCollect = $accModel->hasCollect($q_id);

            $return['problem_info'] = array(
                'id' => $find['id'],
                'problem' => $find['title'],
                'additional' => $find['desc'],
                'asked_head' => $find['head_pic'],
                'asked_nickname' => $find['username'],
                'date_time' => $find['add_time'],
                'collect_state' => (int)$hasCollect,
                'browse_num' => $find['read_num'],
                'class_name_list' => $find['cats_name'],
                'price' => $find['reward_money'],
                'response_time' => $find['response_sec_desc'],
                'state' => $find['open_reward'],
                'question_status_desc' => $find['question_status_desc'],
                'answer_status_desc' => $find['answer_status_desc'],
                'question_status' => $find['question_status'],
                'answer_status' => $find['answer_status'],
                'global_status' => $find['global_status'],
            );
            IF($find['answer_arr']) {
                $hasAdoption = $accModel->hasAdoption($q_id,$find['answer_arr']['au_id']);
                $return['response_info'] = array(
                    'id' => $find['answer_arr']['id'],
                    'price' => $find['answer_arr']['charge_money'],
                    'state' => $find['answer_arr']['charge_view'],
                    'accountant_id' => $find['answer_arr']['au_id'],
                    'accountant_head' => $find['answer_arr']['head_pic'],
                    'accountant_name' => $find['answer_arr']['username'],
                    'accountant_unit' => $find['answer_arr']['work_unit'],
                    'respond_content' => $find['answer_arr']['content'],
                    'date_time' => $find['answer_arr']['add_time'],
                    'support_num' => $find['answer_arr']['adoption_num'],
                    'adoption_state' => (int)$hasAdoption,
                );
            }
        }

        jsonReturn($return);
    }

    /**
     * 会计师列表
     */
    public function accountants(){
        $accModel = new AccountantModel();
        $city = I('post.city');
        $type = I('post.type');
        $page = I('post.page');
        $limit = I('post.limit');
        $time = getPromptDay() * 86400;
        $where = ' status=1 and (expired_time + '.$time.' > '.time().') ';
        if($city){
            $where .= " and city = ".$city;
        }
        if($type){
            $where .= " and find_in_set($type,cat_ids) ";
        }
        $count = $accModel->getList($where,$page,$limit,1);
        $lists = $accModel->getList($where,$page,$limit);

        $return['data'] = $lists;
        $return['page'] = $page;
        $return['total'] = $count;
        jsonReturn($return);

    }

    public function history(){

        $searchModel = new SearchModel();
        $lists = $searchModel->getListByUser();
        jsonReturn(array('data' => $lists));
    }

    public function del_history(){
        $id = I('post.id',0);
        $searchModel = new SearchModel();
        $data = $searchModel->delInfoById($id);
        jsonReturn(array('data' => $data));
    }
}
